﻿using System;
using System.Data;
using System.Text;
using System.Data.SQLite;
namespace Bussiness
{
    /// <summary>
    /// 类Category。
    /// </summary>
    [Serializable]
    public partial class Category
    {
        public Category()
        { }
        #region Model
        private int _id;
        private string _catevalue;
        private string _catelink;
        private int? _type;
        private int? _storeid;
        private int? _seq;
        /// <summary>
        /// 
        /// </summary>
        public int id
        {
            set { _id = value; }
            get { return _id; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string catevalue
        {
            set { _catevalue = value; }
            get { return _catevalue; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string catelink
        {
            set { _catelink = value; }
            get { return _catelink; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int? type
        {
            set { _type = value; }
            get { return _type; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int? storeid
        {
            set { _storeid = value; }
            get { return _storeid; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int? Seq
        {
            set { _seq = value; }
            get { return _seq; }
        }
        #endregion Model


        #region  Method

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Category(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,catevalue,catelink,type,storeid,Seq ");
            strSql.Append(" FROM [Category] ");
            strSql.Append(" where id=@id ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)};
            parameters[0].Value = id;

            DataSet ds = SqliteHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    this.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["catevalue"] != null && ds.Tables[0].Rows[0]["catevalue"].ToString() != "")
                {
                    this.catevalue = ds.Tables[0].Rows[0]["catevalue"].ToString();
                }
                if (ds.Tables[0].Rows[0]["catelink"] != null && ds.Tables[0].Rows[0]["catelink"].ToString() != "")
                {
                    this.catelink = ds.Tables[0].Rows[0]["catelink"].ToString();
                }
                if (ds.Tables[0].Rows[0]["type"] != null && ds.Tables[0].Rows[0]["type"].ToString() != "")
                {
                    this.type = int.Parse(ds.Tables[0].Rows[0]["type"].ToString());
                }
                if (ds.Tables[0].Rows[0]["storeid"] != null && ds.Tables[0].Rows[0]["storeid"].ToString() != "")
                {
                    this.storeid = int.Parse(ds.Tables[0].Rows[0]["storeid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Seq"] != null && ds.Tables[0].Rows[0]["Seq"].ToString() != "")
                {
                    this.Seq = int.Parse(ds.Tables[0].Rows[0]["Seq"].ToString());
                }
            }
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [Category]");
            strSql.Append(" where id=@id ");

            SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)};
            parameters[0].Value = id;

            return SqliteHelper.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [Category] (");
            strSql.Append("catevalue,catelink,type,storeid,Seq)");
            strSql.Append(" values (");
            strSql.Append("@catevalue,@catelink,@type,@storeid,@Seq);select last_insert_rowid() newid;");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@catevalue", DbType.String),
					new SQLiteParameter("@catelink", DbType.String),
					new SQLiteParameter("@type", DbType.Int32,8),
					new SQLiteParameter("@storeid", DbType.Int32,8),
					new SQLiteParameter("@Seq", DbType.Int32,8)};
            parameters[0].Value = catevalue;
            parameters[1].Value = catelink;
            parameters[2].Value = type;
            parameters[3].Value = storeid;
            parameters[4].Value = Seq;

            return SqliteHelper.ExecuteInsert(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [Category] set ");
            strSql.Append("catevalue=@catevalue,");
            strSql.Append("catelink=@catelink,");
            strSql.Append("type=@type,");
            strSql.Append("storeid=@storeid,");
            strSql.Append("Seq=@Seq");
            strSql.Append(" where id=@id ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@catevalue", DbType.String),
					new SQLiteParameter("@catelink", DbType.String),
					new SQLiteParameter("@type", DbType.Int32,8),
					new SQLiteParameter("@storeid", DbType.Int32,8),
					new SQLiteParameter("@Seq", DbType.Int32,8),
					new SQLiteParameter("@id", DbType.Int32,8)};
            parameters[0].Value = catevalue;
            parameters[1].Value = catelink;
            parameters[2].Value = type;
            parameters[3].Value = storeid;
            parameters[4].Value = Seq;
            parameters[5].Value = id;

            int rows = SqliteHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Category] ");
            strSql.Append(" where id=@id ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)};
            parameters[0].Value = id;

            int rows = SqliteHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public void GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,catevalue,catelink,type,storeid,Seq ");
            strSql.Append(" FROM [Category] ");
            strSql.Append(" where id=@id ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)};
            parameters[0].Value = id;

            DataSet ds = SqliteHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    this.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["catevalue"] != null && ds.Tables[0].Rows[0]["catevalue"].ToString() != "")
                {
                    this.catevalue = ds.Tables[0].Rows[0]["catevalue"].ToString();
                }
                if (ds.Tables[0].Rows[0]["catelink"] != null && ds.Tables[0].Rows[0]["catelink"].ToString() != "")
                {
                    this.catelink = ds.Tables[0].Rows[0]["catelink"].ToString();
                }
                if (ds.Tables[0].Rows[0]["type"] != null && ds.Tables[0].Rows[0]["type"].ToString() != "")
                {
                    this.type = int.Parse(ds.Tables[0].Rows[0]["type"].ToString());
                }
                if (ds.Tables[0].Rows[0]["storeid"] != null && ds.Tables[0].Rows[0]["storeid"].ToString() != "")
                {
                    this.storeid = int.Parse(ds.Tables[0].Rows[0]["storeid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Seq"] != null && ds.Tables[0].Rows[0]["Seq"].ToString() != "")
                {
                    this.Seq = int.Parse(ds.Tables[0].Rows[0]["Seq"].ToString());
                }
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM [Category] ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SqliteHelper.Query(strSql.ToString());
        }

        #endregion  Method
    }
}

